package com.school.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.school.pojo.Page;
import com.school.pojo.Student;
import com.school.service.studentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class homeController {

    static public ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    static public studentService service = context.getBean("studentServiceImpl", studentService.class);

    @RequestMapping("/home/tab")
    public String getTab(@RequestParam String page){
        Page page1 = JSON.parseObject(page, Page.class);
        List<Student> li=service.queryStuByPage(page1);
        String list = JSON.toJSONString(li);
        return list;
    }

    @RequestMapping("/home/number")
    public String getNumber(){
        return service.queryNumber()+"";
    }

    @RequestMapping("/home/delTab")
    public String delTab(@RequestParam int id){
        String i = service.delStudent(id)+"";
        return i;
    }

    @RequestMapping("/home/updateTab")
    public String  updateTab(@RequestParam("student") String student){
        Student student1 = JSON.parseObject(student,Student.class);
        int i = service.updateStudent(student1);
        return  i+"";
    }

    @RequestMapping("/home/addTab")
    public String  addTab(@RequestParam("student") String student){
        Student student1 = JSON.parseObject(student,Student.class);
        int i = service.insertStudent(student1);
        return  i+"";
    }

    @RequestMapping("/home/serchTab")
    public String  serchTab(@RequestParam("id") int id,@RequestParam("name") String name,@RequestParam("create_time") String create_time,@RequestParam("end_time") String end_time,
    @RequestParam int currentPage,@RequestParam int pageSize){
        Map<String,Object> map=new HashMap<>();
        if(id!=0) map.put("id",id);
        if(name!="")  map.put("name","%"+name+"%");
        if(create_time!="")  map.put("create_time",create_time);
        if(end_time!="")  map.put("end_time",end_time);
        System.out.println(currentPage);
        map.put("currentPage",(currentPage-1)*pageSize);
        map.put("pageSize",pageSize);
        String list = JSON.toJSONString(service.queryStudent(map));
        System.out.println(map);
        return list;
    }
    // LIMIT #{currentPage},#{pageSize};
    @RequestMapping("/home/serchTabNum")
    public String  serchTabNum(@RequestParam("id") int id,@RequestParam("name") String name,@RequestParam("create_time") String create_time,@RequestParam("end_time") String end_time){
        Map<String,Object> map=new HashMap<>();
        if(id!=0) map.put("id",id);
        if(name!="")  map.put("name","%"+name+"%");
        if(create_time!="")  map.put("create_time",create_time);
        if(end_time!="")  map.put("end_time",end_time);
        int list = service.queryStuByPageNum(map);
        return  list+"";
    }
}
